/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dabarobjects.mobile.ui.actions;

import com.dabarobjects.mobile.bluetooth.PhoneConnectionPort;
import com.dabarobjects.mobile.calls.instructions.PhoneInstructionSets;
import com.dabarobjects.mobile.calls.instructions.phones.SonyEricssonK750SeriesInstruction;
import com.dabarobjects.mobile.flow.CallHandlingProcess;
import com.dabarobjects.mobile.flow.CallStatusListeningTask;
import com.dabarobjects.mobile.ui.models.CallStatusModel;
import com.dabarobjects.ui.surfaces.components.TableControlManager;
import javax.bluetooth.RemoteDevice;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

/**
 *
 * @author DabarObjects Solutions (www.dabarobjects.com)
 */
public class ApplicationServiceManager extends TableControlManager<RemoteDevice> implements ActionResultListener<String> {

    public ApplicationServiceManager(JTable refTable, JPopupMenu nodeContext, JMenuItem nodePointer) {
        super(refTable, nodeContext, nodePointer);
        statusModel = new CallStatusModel();
    }
    private JMenuItem startServiceItem;
    private CreateServicePortAction startServiceAction;

    @Override
    protected void onSelectionMade(RemoteDevice obj) {
        startServiceItem = new JMenuItem();
        startServiceAction = new CreateServicePortAction(obj, this);
        startServiceItem.setAction(startServiceAction);
        startServiceItem.setFont(new java.awt.Font("Trebuchet MS", 0, 12));
        getNodeContext().add(startServiceItem);

    }
    private CallHandlingProcess callStatusHandler;
    private CallStatusListeningTask statusWatcher;
    private String connectionURL;
    private CallStatusModel statusModel;

    public void onActionComplete(String t) {
        if (t != null) {
            try {
                if (!t.equalsIgnoreCase("")) {
                    connectionURL = t;
                    phonePort = new PhoneConnectionPort(t);
                    phonePort.connect();
                    instructionsSets = new SonyEricssonK750SeriesInstruction();
                    statusModel = new CallStatusModel();
                    callStatusHandler = new CallHandlingProcess(instructionsSets, phonePort,statusModel);
                    statusWatcher = new CallStatusListeningTask(phonePort, callStatusHandler, instructionsSets);
                    statusWatcher.addListener(statusModel);
                    updateStatusLabel("Channel is ready, Click Start To Initiate Service");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    private JLabel channelStatusLabel;

    public void updateStatusLabel(String info) {
        if (channelStatusLabel != null) {
            channelStatusLabel.setText(info);
        }
    }

    public void setChannelStatusLabel(JLabel channelStatusLabel) {
        this.channelStatusLabel = channelStatusLabel;
    }

    public CallStatusListeningTask getStatusWatcher() {
        return statusWatcher;
    }

    public String getConnectionURL() {
        return connectionURL;
    }

    public CallStatusModel getStatusModel() {
        return statusModel;
    }
    private PhoneConnectionPort phonePort;
    private PhoneInstructionSets instructionsSets;
}
